var map;
var point = new BMap.Point(119.524111, 39.841867);  // 创建点坐标
var zoom = 15;
var datas = [];
var  mapStyle = {
    features: ["road", "building","water","land"],//隐藏地图上的poi
    style : "midnight"  //设置地图风格为高端黑
};

$(document).ready(function(){
    initMap(null);

});

function initMap(point1){
    map = new BMap.Map("recordMap");
    if (point1 != null){
        map.centerAndZoom(point1,zoom);//设置中心点和级别（级别是1-20）数字越大越是放大
    }else {
        map.centerAndZoom(point,zoom);//设置中心点和级别（级别是1-20）数字越大越是放大
    }
    map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放
    map.setMapStyle(mapStyle);
    map.addEventListener("tilesloaded", showRoads);

    initPolyLine(datas);
    addMarkers(datas);
    initLuShu(datas);
}


function showRoads(){
    map.removeEventListener("tilesloaded", showRoads);
}


// 地图上添加显示的点位
function addMarkers(datas){
    var markers = getMarkers(datas);
    for(dataIndex in datas){
        map.addOverlay(datas[dataIndex]);//添加站点marker
    }
}

//初始化需要显示的点位
function getMarkers(datas){
    var markers = [];
    for(dataIndex in datas){
        if(datas[dataIndex].isMarker){
            var icon = new BMap.Icon(datas[dataIndex].icon, new BMap.Size(32,32),{anchor: new BMap.Size(16, 32)});//声明站点标注
            markers.push(new BMap.Marker(new BMap.Point(datas[dataIndex].lng, datas[dataIndex].lat),{icon:icon}));
        }
    }
    return markers;
}


function initPolyLine(datas){
    var points = getPoint(datas);
    var sy = new BMap.Symbol(BMap_Symbol_SHAPE_BACKWARD_OPEN_ARROW, {
        scale: 0.6,//图标缩放大小
        strokeColor:'#fff',//设置矢量图标的线填充颜色
        strokeWeight: '2',//设置线宽
    });
    var icons = new BMap.IconSequence(sy, '10', '30');
    var polyline = new BMap.Polyline(points, {
        enableEditing: false,//是否启用线编辑，默认为false
        enableClicking: true,//是否响应点击事件，默认为true
        icons:[icons],
        strokeColor:"#5298ff", //折线颜色
        strokeWeight:6, //折线的宽度，以像素为单位
        strokeOpacity:0.8 //折线的透明度，取值范围0 - 1
    });
    map.addOverlay(polyline);//添加轨迹到地图
}

//初始化线路点位
function getPoint(datas){
    var points = [];
    for(dataIndex in datas){
        points.push(new BMap.Point(datas[dataIndex].lng, datas[dataIndex].lat));
    }
    return points;
}

function initLuShu(datas){
    var lushu = new BMapLib.LuShu(map, getPoint(datas), {
        //landmarkPois:此参数是路书移动的时候碰到这个点会触发pauseTime停留中设置的时间，单位为秒，经纬度误差超过十米不会停止
        landmarkPois:getLandmarkPois(datas),
        defaultContent: '行车轨迹 ...',
        speed: 100,//速度，单位米每秒
        /* 1、需要把图片和代码放在同一个文件夹下面
        * 2、size()是设置图片大小，图片过大可以截取
        * 3、anchor是设置偏移，默认是图片最中间，设置偏移目的是让图片底部中间与坐标重合
        */
        icon: new BMap.Icon('/images/car.png', new BMap.Size(48, 48), {anchor: new BMap.Size(24, 34)}),//声明高铁标注
        autoView: false,
        enableRotation: false
    });
    lushu.start();
}

function getLandmarkPois(datas){
    var points = [];
    for(dataIndex in datas){
        if(datas[dataIndex].isMarker){
            points.push(datas[dataIndex]);
        }
    }
    return points;
}


function loadRecordMap() {
    var plateNo = $("#plateNo").val();
    var parkId = $("#parkId").val();
    $.ajax({
        url:"/front/getPassRecordByPlateMarker",
        type:"GET",
        data:{"plateNo":plateNo,"parkId":parkId},
        dataType:"JSON",
        //async:true,
        success:function(data){
            if(data.length > 0){
                var point1 ;
                for(var d in data){
                    var point = {};
                    point.lng = data[d].lng;
                    point.lat = data[d].lat;
                    point.icon = '/images/car.png';
                    point.pauseTime = 3;
                    point.isMarker = true;
                    point.html = "<img class='mapIImmgg' src=\""+data[d].bigImage+"\"></br>"+data[d].license;
                    datas.push(point);
                    point1 = new BMap.Point(data[d].lng, data[d].lat);  // 创建点坐标
                }
                console.log("开始加载地图点位信息");
                console.log(datas);
                initMap(point1);
            }
        }
    });
}
